<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.prevent.native>
      <el-form-item :label="$t('指标名称')" prop="keyName">
        <el-input
          v-model="queryParams.keyName"
          :placeholder="$t('指标名称')"
          clearable
          :onsubmit="false"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('重点关注')" prop="focusOn">
        <el-select v-model="queryParams.focusOn" filterable :placeholder="$t('重点关注')" clearable>
          <el-option
            v-for="dict in focusOnOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['company:pcw:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          v-hasPermi="['company:pcw:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="pcwTempSysKeyList"
      default-expand-all
      row-key="id"
      border
    >
      <el-table-column :label="$t('分类')" width="160" align="left" prop="keyReport">
        <template slot-scope="scope">
          <span v-if="scope.row.id && scope.row.pid === '0'" :class="{fontWeightClass: scope.row.focusOn === 'Y'}">{{ $t(scope.row.keyReport) }}</span>
          <el-input v-if="!scope.row.id" v-model="scope.row.keyReport" :placeholder="$t('分类')" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('指标名称')" width="250" align="left" prop="keyName">
        <template slot-scope="scope">
          <span v-if="scope.row.id " :class="{fontWeightClass: scope.row.focusOn === 'Y'}">{{ $t(scope.row.keyName) }}</span>
          <el-input v-if="!scope.row.id" v-model="scope.row.keyName" :placeholder="$t('指标名称')" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('公式')" align="left" prop="indicatorsFormula">
        <template slot-scope="scope">
          <span v-if="scope.row.id " :class="{fontWeightClass: scope.row.focusOn === 'Y'}">{{ $t(scope.row.indicatorsFormula) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template v-if="scope.row.keyName || !scope.row.id" slot-scope="scope">
          <el-button
            v-hasPermi="['company:pcw:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['company:pcw:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改关键指标公式（系统模板）对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <pcwTempSysKeyForm ref="pcwTempSysKeyForm" :type-id="typeId" />
    </el-dialog>
  </div>
</template>

<script>
import { listPcwTempSysKey, delPcwTempSysKey, exportPcwTempSysKey } from '@/api/company/pcw/pcwTempSysKey'
import pcwTempSysKeyForm from './pcwTempSysKeyForm.vue'
export default {
  components: {
    pcwTempSysKeyForm
  },
  props: {
    typeId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 关键指标公式（系统模板）表格数据
      pcwTempSysKeyList: [],
      focusOnOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        typeId: this.typeId,
        keyCode: undefined,
        keyName: undefined,
        focusOn: undefined,
        keyReport: undefined,
        indicatorsFormula: undefined,
        indicatorsCodeFormula: undefined,
        sort: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  watch: {
    companyId: {
      handler(val) {
        if (val) {
          this.queryParams.typeId = val
          this.getList()
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    this.getDicts('sys_yes_no').then(response => {
      this.focusOnOptions = response.data
    })
  },
  mounted() {
    if (this.$route.query.typeId) {
      this.queryParams.typeId = this.$route.query.typeId
    }
    this.getList()
  },
  methods: {
    /** 查询关键指标公式（系统模板）列表 */
    getList() {
      this.loading = true
      listPcwTempSysKey(this.queryParams).then(response => {
        this.pcwTempSysKeyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        typeId: this.typeId,
        keyCode: undefined,
        keyName: undefined,
        keyReport: undefined,
        indicatorsFormula: undefined,
        indicatorsCodeFormula: undefined,
        sort: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.title = '关键指标（系统模板）'
      this.$nextTick(() => {
        this.$refs.pcwTempSysKeyForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.title = '关键指标（系统模板）'
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.pcwTempSysKeyForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delPcwTempSysKey(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return exportPcwTempSysKey(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    }
  }
}
</script>
<style scoped lang="scss">
/deep/ .fontWeightClass {
  font-weight: bold;
}
</style>
